package com.maori.languageapp;

import java.util.List;

import com.maori.languageapp.adapter.LanguageAppDialetDataList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;

public class LanguageAppDialetAdapterList extends ArrayAdapter<LanguageAppDialetDataList> {

    Context context;
    ViewHolder holder;
    private boolean userSelected = false;
    private RadioButton mCurrentlyCheckedRB;

    public LanguageAppDialetAdapterList(Context context, int textViewResourceId,
            List<LanguageAppDialetDataList> items) {
        super(context, textViewResourceId, items);
        this.context = context;
    }

    private class ViewHolder {
        RadioButton radioBtn;
        Button btAdd;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        holder = null;
        LanguageAppDialetDataList rowItem = getItem(position);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.language_app_dialect_listview_raw, null);
            holder = new ViewHolder();
            holder.radioBtn = (RadioButton) convertView
                    .findViewById(R.id.rdb_payment_method);
            holder.btAdd = (Button) convertView
                    .findViewById(R.id.bt_item_event);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        if (position == getCount() - 1 && userSelected == false) {
            holder.radioBtn.setChecked(true);
            mCurrentlyCheckedRB = holder.radioBtn;
        } else {
            holder.radioBtn.setChecked(false);
        }

        holder.radioBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (mCurrentlyCheckedRB != null) {
                    if (mCurrentlyCheckedRB == null)
                        mCurrentlyCheckedRB = (RadioButton) v;
                    mCurrentlyCheckedRB.setChecked(true);
                }

                if (mCurrentlyCheckedRB == v)
                    return;

                mCurrentlyCheckedRB.setChecked(false);
                ((RadioButton) v).setChecked(true);
                mCurrentlyCheckedRB = (RadioButton) v;
            }
        });

        holder.radioBtn.setText(rowItem.getRdText());
        holder.btAdd.setText(rowItem.getBtText());
        return convertView;
    }

}